package com.clkj.sunhelp.mvp.publicityshow.publicityofapplyobj;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForPublicityOfApplyObj;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.PublicityOfApplyObjItem;
import com.clkj.sunhelp.common.Constant;
import com.clkj.sunhelp.common.IntentKey;
import com.clkj.sunhelp.mvp.base.BaseFragment;
import com.clkj.sunhelp.mvp.publicityshow.publicityofapplyobjdetail.ActivityPublicityOfApplyObjDetail;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/26.
 */

public class FragmentPublicityOfApplyObjList extends BaseFragment implements PullToRefreshBase.OnRefreshListener2, PublicityOfApplyObjContract.View {

    public static final String PUBLICITY_TYPE = "publicity_type";
    public static final String PUBLICITY_TYPE_NAME = "publicity_type_name";

    public String mPublicityType;
    public String mPublicityTypeName;

    public static FragmentPublicityOfApplyObjList getInstance(String publicityTypeName,String publicityType) {
        Bundle bundle = new Bundle();
        bundle.putString(PUBLICITY_TYPE, publicityType);
        bundle.putString(PUBLICITY_TYPE_NAME, publicityTypeName);
        FragmentPublicityOfApplyObjList fragment = new FragmentPublicityOfApplyObjList();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mPublicityType = bundle.getString(PUBLICITY_TYPE);
            mPublicityTypeName = bundle.getString(PUBLICITY_TYPE_NAME);
        }

        mPresenter = new PublicityOfApplyObjPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    private PullToRefreshListView lv;
    private ListView originalLv;
    private RelativeLayout layoutEmpty;
    private TextView tvEmptytip;


    private Integer mPageIndex = 1;
    private boolean isRefresh;
    private boolean canLoad;


    List<PublicityOfApplyObjItem> mPublicityList = new ArrayList<>();
    ListAdapterForPublicityOfApplyObj mAdapter;

    PublicityOfApplyObjContract.Presenter mPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment_publicity_apply_obj_list_view, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        assignView(view);
        initView();
    }

    @Override
    public void initData() {

        /*PublicityOfApplyObjItem publicityOfApplyObjItem = new PublicityOfApplyObjItem();
        publicityOfApplyObjItem.setTitle("公示标题公示标题公示标题公示标题公示标题");
        publicityOfApplyObjItem.setTime("公示时间:2017-03-27 10:30");
        mPublicityList.add(publicityOfApplyObjItem);
        mPublicityList.add(publicityOfApplyObjItem);
        mPublicityList.add(publicityOfApplyObjItem);
        mPublicityList.add(publicityOfApplyObjItem);
        mPublicityList.add(publicityOfApplyObjItem);
        mPublicityList.add(publicityOfApplyObjItem);
        mPublicityList.add(publicityOfApplyObjItem);
        mPublicityList.add(publicityOfApplyObjItem);*/

    }

    @Override
    public void assignView(View view) {
        lv = (PullToRefreshListView) view.findViewById(R.id.lv);
        originalLv = lv.getRefreshableView();

        layoutEmpty = (RelativeLayout) view.findViewById(R.id.layout_empty);
        tvEmptytip = (TextView) view.findViewById(R.id.tv_emptytip);
    }

    @Override
    public void initView() {
        lv.setOnRefreshListener(this);
        lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        mAdapter = new ListAdapterForPublicityOfApplyObj(getActivity(), mPublicityList);
        originalLv.setAdapter(mAdapter);
        originalLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent toPublicityDetail = new Intent(getActivity(), ActivityPublicityOfApplyObjDetail.class);
                toPublicityDetail.putExtra(IntentKey.INTENT_KEY_PUBLICITY_OF_APPLY_OBJ_ITEM, mPublicityList.get(position-1));
                startActivity(toPublicityDetail);


            }
        });
    }

    @Override
    public void loadData() {
        isRefresh = true;
        mPageIndex = 1;
        //获取数据
        getPublicityOfApplyObjList();
    }


    public void loadMoreData() {
        if (canLoad) {
            mPageIndex++;
            //加载更多数据
            getPublicityOfApplyObjList();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        loadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        loadMoreData();
    }

    @Override
    public void getPublicityOfApplyObjList() {
        mPresenter.getPublicityOfApplyObjList(mPublicityType, mPageIndex.toString(), Constant.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void onGetPublicityOfApplyObjListSuccess(List<PublicityOfApplyObjItem> publicityList) {
        lv.onRefreshComplete();

        if (isRefresh) {
            mPublicityList.clear();
            isRefresh = false;
        }
        if (publicityList.size() > 0) {
            if (publicityList.size() >= Constant.DEFAULT_PAGE_SIZE) {
                lv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            mPublicityList.addAll(publicityList);

        } else {
            canLoad = false;
            lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (mPublicityList.size() == 0) {
                lv.setEmptyView(layoutEmpty);
            } else {
                ToastUtil.show(getActivity(), "已经获取全部数据", 2000);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetPublicityOfApplyObjListError(String tip) {
        lv.onRefreshComplete();
        ToastUtil.showShort(getActivity(), tip);
        if (isRefresh) {
            mPublicityList.clear();
            lv.setEmptyView(layoutEmpty);

        }

        isRefresh = false;
        canLoad = false;

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(PublicityOfApplyObjContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false, null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }
}
